//采购合同子项号、资源号、捆包号、入库状态、件数、新件数、
//入库量、新入库量、炉号、库位、厚、宽、长、入库单子项号、
//采购合同子项状态、品名、牌号、规格、产地、技术标准、质量等级、货主单位、备注。
function getStorageFileEditSubOptions() {
    var initOptions = {
        idField: 'cid',
        fitColumns: false,
        checkOnSelect: false,
        singleSelect: false,
        fit: true,
        columns: [[{
            checkbox:true
        },{
            field: 'contractSubCode',
            title: '采购合同子项号',
            width: 110
        }, {
            title: '资源号',
            field: 'resourceNumber',
            width: 60
        }, {
            title: '捆包号',
            field: 'packageNumber',
            width: 110
        }, {
            title: '入库状态',
            field: 'storageSubStatus',
            width: 60
        }, {
            field: 'storagePage',
            title: '件数',
            width: 60,
            align: 'right'
        }, {
            field: 'newStoragePage',
            title: '<label class="redSharp">*新件数</label>',
            width: 60,
            align: 'right',
            editor:{
                type:'numberbox',
                options: {
                    required: true
                }
            }
        }, {
            title: '入库量',
            field: 'storageNo',
            width: 80,
            align: 'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatWeight(value);
            }
        },{
            title: '<label class="redSharp">*新入库量</label>',
            field: 'newStorageNo',
            width: 80,
            editor: {
                type: 'numberbox',
                options: {
                    precision: 6,
                    required: true
                }
            },
            align: 'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatWeight(value);
            }
        }, {
            field: 'furnaceNo',
            title: '炉号',
            width: 60
        }, {
            title: '库位',
            field: 'posation',
            width: 60
        }, {
            title: '厚',
            field: 'mdThick',
            width: 60
        }, {
            title: '宽',
            field: 'mdWidths',
            width: 60
        }, {
            title: '长',
            field: 'mdLong',
            width: 60
        }, {
            title: '入库单子项号',
            field: 'storageSubCode',
            width: 110
        }, {
            title: '采购合同子项状态',//TODO node
            field: 'contractSubStatus',
            width: 110
        }, {
            title: '品名',
            field: 'goodsName',
            width: 80
        }, {
            title: '牌号',
            field: 'marks',
            width: 80
        }, {
            title: '规格',
            field: 'cpspecification',
            width: 110
        }, {
            title: '产地',
            field: 'cdName',
            width: 80
        }, {
            title: '技术标准',
            field: 'standards',
            width: 80
        }, {
            title: '质量等级',
            field: 'qualityGrade',
            width: 80
        }, {
            title: '货主单位',
            field: 'cargoUnits',
            width: 60
        }, {
            title: '备注',
            field: 'remark',
            width: 80
        }]],
        onClickCell: onClickCell,
        onAfterEdit: function (rowIndex, rowData, changes) {
            //calculateStorageTop();
        },
        onCheck:function(rowIndex,rowData){
            var nodeArray = [$("#saveStorageDoc"), $("#cancelStorageDocSub")];
            if($('#storageStatus').text()==="1"&&rowData.storageSubStatus ==="确认"){
                setLinkButtonStatus(nodeArray, [1, 1]);
            }else{
                setLinkButtonStatus(nodeArray, [0, 0]);
            }
        }
    }
    return initOptions;
}

function onClickCell(index, field) {
    if (endEditing($('#subItem'))) {
        if (checkLineStatus($('#subItem'), index)) {
            $('#subItem').datagrid('selectRow', index)
                .datagrid('editCell', {index: index, field: field});
            var ed = $('#subItem').datagrid('getEditor', {index: index, field: field});
            if (ed && ed.target) {
                $(ed.target).focus();
            }
            editIndex = index;
        }
    }
}

function checkLineStatus(node, index) {
    return true;
}

function validateDocViewBottomSubs(data) {
    if (data.length > 0) {
        var str = "";
        $.each(data, function (index, value) {
            var hangNo = index + 1;
            if (isEmpty(value.newStoragePage)||value.newStoragePage<0) {
                str += "第 " + hangNo + " 行" + "新件数为非空正数" + "<br/>";
            }
            if (isEmpty(value.newStorageNo)||value.newStorageNo<0) {
                str += "第 " + hangNo + " 行" + "新入库量为非空正数" + "<br/>";
            }
        });
        if (str === "") {
            str = "true";
        }
        return str;
    } else {
        return "必须包含有效子项";
    }
}

//新件数 新入库量赋值 增加uuid
function initStorageSubNew() {
    var rows = $('#subItem').datagrid('getRows');
    $.each(rows, function (index, value) {
        value.newStoragePage = value.storagePage;
        value.newStorageNo = value.storageNo;
        value.cid = UUID();
        // 刷新该行, 只有刷新了才有效果
        $('#subItem').datagrid('refreshRow', index);
    })
}


function prepareStorageFileSubmitPostData() {
    endEditing($('#subItem'));
    var payData = {};
    //子项表数据
    var subRows = $('#subItem').datagrid('getChecked');
    //数据主体结构
    payData.storage = {};
    payData.storageSubs = subRows;
    return payData;
}

function prepareStorageFileSubmitRealPostData() {
    var data = prepareStorageFileSubmitPostData();
    data.storage.id = $('#storageId').text();
    var storageSubs = data.storageSubs;
    $.each(storageSubs, function (index, value) {
        data.storageSubs[index].storagePage = value.newStoragePage;
        data.storageSubs[index].storageNo = value.newStorageNo;
        delete value.cid;
        delete value.newStoragePage;
        delete value.newStorageNo;
    });
    data.storageSubs =storageSubs;
    return data;
}

function refreshBackStorageDocEditList(data){
    if(data&&!isSuccess(data.result)){
        errorFunction(data);
        $('#subItem').datagrid('uncheckAll');
    }else{
        var storageId = $('#storageId').text()
        parent.$.modalDialog.handler.dialog('close');
        changeStorageFileFunction(storageId);
        $('#subItem').datagrid('uncheckAll');
    }
    //var dataS = getStatusData("/storage/selectStorageSubList.html?storageId=" + $('#storageId').text());
    //refreshStorageFileListPage(dataS,getStorageFileEditSubOptions());
    //initStorageSubNew();

}

function refreshStorageDocEditList(data){
    var dataS = getStatusData("/storage/selectStorageSubList.html?storageId=" + $('#storageId').text());
    refreshStorageFileListPage(dataS,getStorageFileEditSubOptions());
    initStorageSubNew();
    $('#subItem').datagrid('uncheckAll');
    if(data&&!isSuccess(data.result)){
        errorFunction(data);
    }
}